import { datetime } from '@/utils'

export default {
  data() {
    return {
      tableHeight: window.innerHeight - 300,
      loading: false,
      columns: [],
      // 表格数据
      tableData: [],
      selectOptions: [],
      // 分页
      pageNo: 1,
      pageSize: 10,
      pageSizes: [10, 20, 30, 40, 50],
      totalSize: 0,
      searchParams: {
      },
      defaultParams: {},
      // 弹窗
      dialogText: '',
      dialogValue: false,
      currentDetail: {},
      ruleForm: {},
      resetForm: {},
      rules: {},
      // 方法
      request: null,
      deleteKey: 'id',
      manual: false,
    }
  },
  mounted() {
    this.initSetting()
    if (this.manual) return;
    this.initData()
  },
  methods: {
    /**
     *  初始化设置 在获取分页数据前调用
     */
    initSetting() {},
    initData() {
      if (!this.request || this.loading) return
      if (this.request.page) {
        this.loading = true
        this.request.page(this.getQueryParams()).then(res => {
          this.loading = false
          this.totalSize = res.data.total
          const tableData = res.data.list
          if (this.formatTableData) {
            this.tableData = this.formatTableData(tableData)
            return
          }
          this.tableData = tableData
          console.log(this.tableData, '----')
        }).catch(() => this.loading = false)
      } else if (this.request.list || this.request.tree) {
        this.loading = true
        this.request.list(this.getQueryParams2()).then(res => {
          this.loading = false
          const tableData = res.data
          this.totalSize = res.data.length
          if (this.formatTableData) {
            this.tableData = this.formatTableData(tableData)
            return
          }
          this.tableData = tableData
        }).catch(() => this.loading = false)
      }
    },
    formatTableData(data) {
      data.forEach(item => {
        for (const key in item) {
          if (item[key] === 'Invalid Date') {
            item[key] = '-'
          }
          if (key.includes('Time')) {
            item[key] = datetime(item[key])
          }
        }
      })
      return data
    },

    getQueryParams() {
      const params = {
        pageNo: this.pageNo,
        pageSize: this.pageSize,
        ...this.defaultParams
      }
      if (!this.searchParams) return params
      for (const searchParamsKey in this.searchParams) {
        if (searchParamsKey === 'timeValue') {
          if (!this.searchParams[searchParamsKey] || !this.searchParams[searchParamsKey].length) {
            this.searchParams[searchParamsKey] = ['', '']
          }
          params['beginTime'] = this.searchParams.timeValue[0] || undefined
          params['endTime'] = this.searchParams.timeValue[1] || undefined
        } else {
          params[searchParamsKey] = this.searchParams[searchParamsKey]
        }
      }
      console.log('params',params)
      return params
    },
    getQueryParams2() {
      const params = {
        ...this.defaultParams
      }
      if (!this.searchParams) return params
      for (const searchParamsKey in this.searchParams) {
        if (searchParamsKey === 'timeValue') {
          if (!this.searchParams[searchParamsKey] || !this.searchParams[searchParamsKey].length) {
            this.searchParams[searchParamsKey] = ['', '']
          }
          params['beginTime'] = this.searchParams.timeValue[0] || undefined
          params['endTime'] = this.searchParams.timeValue[1] || undefined
        } else {
          params[searchParamsKey] = this.searchParams[searchParamsKey]
        }
      }
      return params
    },
    onSearch() {
      this.pageNo = 1
      this.initData()
    },
    onReset() {
      for (const key in this.searchParams) {
        switch (key) {
          case 'timeValue':
            this.searchParams.timeValue = []
            break
          default:
            this.searchParams[key] = ''
        }
      }
      this.searchParams = {
        ...this.defaultParams
      }
      this.onSearch()
    },
    onSelect(options) {
      this.selectOptions = options
    },
    onAdd() {
      this.$refs.addFormRef.open()
    },
    onEdit(item) {
      this.$refs.addFormRef.open(item)
    },
    onDelete(row) {
      this.$confirm('确认删除？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.request.delete(row[this.deleteKey]).then(res => {
            this.$message.success('删除成功')
            this.onSearch()
          })
        })
        .catch(() => {

        })
    },
    changePageNo(pageNo) {
      this.pageNo = pageNo
      this.initData()
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize
      this.pageNo = 1
      this.initData()
    },
    dateFormatter(date) {
      datetime(date)
    }
  }
}
